<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="mobile-web-app-capable" content="yes">
  <meta name="HandheldFriendly" content="true"/>

  <title>REST Text iink</title>

  <link rel="stylesheet" href="../../examples.css">

  <script src="https://code.jquery.com/pep/0.4.3/pep.js"></script>
  <script type="text/javascript" src="../../../dist/iink.min.js"></script>
</head>

<body>
<div id="result"></div>
<div>
  <nav>
    <div class="button-div">
      <button id="clear" class="nav-btn btn-fab-mini btn-lightBlue" disabled>
        <img src="../../assets/img/clear.svg">
      </button>
      <button id="undo" class="nav-btn btn-fab-mini btn-lightBlue" disabled>
        <img src="../../assets/img/undo.svg">
      </button>
      <button id="redo" class="nav-btn btn-fab-mini btn-lightBlue" disabled>
        <img src="../../assets/img/redo.svg">
      </button>
    </div>
    <select id="language"></select>
  </nav>
  <div id="editor" touch-action="none"></div>
</div>
<script>
  const editorElement = document.getElementById('editor');
  const resultElement = document.getElementById('result');
  const languageElement = document.getElementById('language');
  const undoElement = document.getElementById('undo');
  const redoElement = document.getElementById('redo');
  const clearElement = document.getElementById('clear');

  editorElement.addEventListener('changed', (event) => {
    undoElement.disabled = !event.detail.canUndo;
    redoElement.disabled = !event.detail.canRedo;
  });

  editorElement.addEventListener('exported', (evt) => {
    const exports = evt.detail.exports;
    if (exports && exports['text/plain']) {
      clearElement.disabled = false;
      resultElement.innerHTML = '<span>' + exports['text/plain'] + '</span>';
    } else {
      clearElement.disabled = true;
      resultElement.innerHTML = '';
    }
  });

  editorElement.addEventListener('loaded', async (evt) => {
    /**
     * Retrieve the list of available recognition languages
     * @param {Object} The editor recognition parameters
     */
    const currentLanguage = evt.target.editor.configuration.recognitionParams.iink.lang;
    const res = await iink.getAvailableLanguageList(evt.target.editor.configuration);

    if (languageElement.options.length === 0) {
      Object.keys(res.result).forEach(function (key) {
        const selected = currentLanguage === key;
        languageElement.options[languageElement.options.length] = new Option(res.result[key], key, selected, selected);
      });
    }
  });

    languageElement.addEventListener('change', (e) => {
    const configuration = editorElement.editor.configuration;
    //The path to the language depend of the version of API you are using.
    configuration.recognitionParams.iink.lang = e.target.value;
    resultElement.innerHTML = '';
  });

  undoElement.addEventListener('click', () => {
    editorElement.editor.undo();
  });
  redoElement.addEventListener('click', () => {
    editorElement.editor.redo();
  });
  clearElement.addEventListener('click', () => {
    editorElement.editor.clear();
  });


  /**
   * Attach an editor to the document
   * @param {Element} The DOM element to attach the ink paper
   * @param {Object} The recognition parameters
   */
  iink.register(editorElement, {
    triggers: {
      exportContent: 'QUIET_PERIOD'
    },
    recognitionParams: {
      type: 'TEXT',
      protocol: 'REST',
      server: {
        scheme: 'https',
        host: 'webdemoapi.myscript.com',
        applicationKey: '515131ab-35fa-411c-bb4d-3917e00faf60',
        hmacKey: '54b2ca8a-6752-469d-87dd-553bb450e9ad'
      },
      iink: {
        text: {
          guides: {
            enable: false
          },
          mimeTypes: ['text/plain', 'application/vnd.myscript.jiix'],
        }
      }
    }
  });

  window.addEventListener('resize', () => {
    editorElement.editor.resize();
  });
</script>
</body>

</html>